package com.ruoyi.project.mini.goods.controller;

import com.ruoyi.project.lely.bean.dto.LyStatusDTO;
import com.ruoyi.project.system.user.domain.User;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;

import com.ruoyi.project.mini.goods.domain.Minigoods;
import com.ruoyi.project.mini.goods.service.IMinigoodsService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 商品基本信息Controller
 *
 * @author luoyong
 * @date 2022-01-21
 */
@Controller
@RequestMapping("/mini/goods")
public class MinigoodsController extends BaseController {

    private String prefix = "mini/goods";

    @Autowired
    private IMinigoodsService minigoodsService;

    @RequiresPermissions("mini:goods:view")
    @GetMapping()
    public String goods() {
        return prefix + "/goods";
    }

    /**
     * 查询商品基本信息列表
     */
    @RequiresPermissions("mini:goods:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Minigoods minigoods) {
        startPage();
        List<Minigoods> list = minigoodsService.selectMinigoodsList(minigoods);
        return getDataTable(list);
    }

    /**
     * 导出商品基本信息列表
     */
    @RequiresPermissions("mini:goods:export")
    @Log(title = "商品基本信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Minigoods minigoods) {
        List<Minigoods> list = minigoodsService.selectMinigoodsList(minigoods);
        ExcelUtil<Minigoods> util = new ExcelUtil<Minigoods>(Minigoods.class);
        return util.exportExcel(list, "商品基本信息数据");
    }

    /**
     * 新增商品基本信息
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存商品基本信息
     */
    @RequiresPermissions("mini:goods:add")
    @Log(title = "商品基本信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Minigoods minigoods) {
        return toAjax(minigoodsService.insertMinigoods(minigoods));
    }

    /**
     * 修改商品基本信息
     */
    @RequiresPermissions("mini:goods:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id,
            ModelMap mmap) {
        Minigoods minigoods =
                minigoodsService.selectMinigoodsById(id);
        mmap.put("minigoods", minigoods);
        return prefix + "/edit";
    }

    /**
     * 修改保存商品基本信息
     */
    @RequiresPermissions("mini:goods:edit")
    @Log(title = "商品基本信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Minigoods minigoods) {
        return toAjax(minigoodsService.updateMinigoods(minigoods));
    }

    /**
     * 删除商品基本信息
     */
    @RequiresPermissions("mini:goods:remove")
    @Log(title = "商品基本信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(minigoodsService.deleteMinigoodsByIds(ids));
    }

    /**
     * 禁用启用
     */
    @Log(title = "禁用启用", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:user:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(@Validated LyStatusDTO dto) {
        return toAjax(minigoodsService.changeStatus(dto));
    }
}
